import { isEmpty, isString, assert, reverseMerge } from '../utils'
import Condition from './condition'

export default class CUDBuilder {
  table
  static methods = ['post', 'put', 'delete']
  data = {}
  tag

  constructor(table) {
    assert(
      !isEmpty(table) && isString(table),
      `[ApiJson] 参数table: ${table} 非法`
    )
    this.table = table
  }

  static by(table) {
    return new CUDBuilder(table)
  }

  set(field, value) {
    assert(!isEmpty(field) && isString(field), `[ApiJson] ${field} 非法`)
    this.data[field] = value
    return this
  }
  setData(data) {
    assert(!isEmpty(data), `[ApiJson] ${data} 非法`)
    this.data = data
    return this
  }
  whereId(expr, value) {
    const condition = Condition.by('id', expr, value)
    reverseMerge(this.data, condition.toJson())
    return this
  }

  setTag(tag) {
    assert(!isEmpty(tag) && isString(tag), `[ApiJson] 参数tag: ${tag} 非法`)
    this.tag = tag
    return this
  }

  toJson() {
    const { table, tag, data } = this
    const json = {}
    json[table] = {}
    assert(tag, `[ApiJson] 参数tag: ${tag}参数非法`)
    assert(Object.keys(data).length > 0, `[ApiJson] 参数data: ${data} 参数非法`)

    reverseMerge(json[table], data)
    json.tag = tag
    return json
  }

  // send() {
  //     const params = this.toJson()
  //     const {method} = this;
  //     return request({
  //         url: '/' + method,
  //         method: 'post',
  //         data: params
  //     })
  // }
}
